﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Xioa_UI.Controls;

[TemplatePart(Name = BackButton, Type = typeof(Button))]
public class XioaPageHeaderControl : ContentControl
{
    public const string BackButton = "BackButton";

    public Button? _button;

    public static readonly DependencyProperty HeaderProperty =
        ElementBase.Property<XioaPageHeaderControl, string>(nameof(HeaderProperty));

    public string? Header
    {
        get => (string)GetValue(HeaderProperty);
        set => SetValue(HeaderProperty, value);
    }

    public static readonly DependencyProperty BackButtonContentProperty =
        ElementBase.Property<XioaPageHeaderControl, string>(nameof(BackButtonContentProperty));

    public string BackButtonContent
    {
        get => (string)GetValue(BackButtonContentProperty);
        set => SetValue(BackButtonContentProperty, value);
    }

    public static readonly DependencyProperty SeparatorHeightProperty =
        ElementBase.Property<XioaPageHeaderControl, double>(nameof(SeparatorHeightProperty));

    public double SeparatorHeight
    {
        get => (double)GetValue(SeparatorHeightProperty);
        set => SetValue(SeparatorHeightProperty, value);
    }

    public override void OnApplyTemplate()
    {
        if (_button is not null)
            _button.Click -= OnButtonClick;
        
        _button = GetTemplateChild(BackButton) as Button;

        if (_button is not null)
        {
            _button.Click += OnButtonClick;
            _button.Command = Command;
            _button.CommandParameter = CommandParameter;
            _button.CommandTarget = CommandTarget;
        }
    }

    public void OnButtonClick(object sender, RoutedEventArgs e)
    {
        var newEventArgs = new RoutedEventArgs(XioaPageHeaderControl.ClickEvent);
        RaiseEvent(newEventArgs);
    }

    public event RoutedEventHandler Click
    {
        add => AddHandler(ClickEvent, value);
        remove => RemoveHandler(ClickEvent, value);
    }

    public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent("ClickEvent",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(XioaPageHeaderControl));

    public static readonly DependencyProperty CommandProperty =
        ElementBase.Property<XioaPageHeaderControl, ICommand>(nameof(CommandProperty));

    public ICommand Command
    {
        get => (ICommand)GetValue(CommandProperty);
        set => SetValue(CommandProperty, value);
    }

    public static readonly DependencyProperty CommandParameterProperty =
        ElementBase.Property<XioaPageHeaderControl, object>(nameof(CommandParameterProperty));

    public object CommandParameter
    {
        get => (object)GetValue(CommandParameterProperty);
        set => SetValue(CommandParameterProperty, value);
    }

    public static readonly DependencyProperty CommandTargetProperty =
        ElementBase.Property<XioaPageHeaderControl, IInputElement>(nameof(CommandTargetProperty));

    public IInputElement CommandTarget
    {
        get => (IInputElement)GetValue(CommandTargetProperty);
        set => SetValue(CommandTargetProperty, value);
    }
}